<template>
  <div class="login">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      class="demo-ruleForm"
    >
      <el-form-item prop="email">
        <el-input
          v-model="ruleForm.email"
          type="text"
          autocomplete="off"
          placeholder="QQ邮箱"
        />
      </el-form-item>
      <el-form-item prop="Code">
        <el-input
          v-model="ruleForm.Code"
          type="text"
          autocomplete="off"
          placeholder="验证码"
        />
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input
          v-model="ruleForm.checkPass"
          type="password"
          autocomplete="off"
          placeholder="密码"
        />
      </el-form-item>
      <el-form-item prop="chenksPwd">
        <el-input
          v-model="ruleForm.chenksPwd"
          type="password"
          autocomplete="off"
          placeholder="确认密码"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >注册</el-button
        >
        <el-button @click="resetForm">我要登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, reactive } from "vue";
import type { FormInstance } from "element-plus";

export default defineComponent({
  setup(prop, context) {
    const ruleFormRef = ref<FormInstance>();

    const validatePass = (rule: any, value: any, callback: any) => {
      const reg = /^[1-9][0-9]{4,10}@qq.com$/;
      if (value === "") {
        callback(new Error("qq邮箱不能为空"));
      } else if (!reg.test(value)) {
        callback(new Error("请输入qq邮箱"));
      } else {
        if (ruleForm.checkPass !== "") {
          if (!ruleFormRef.value) return;
          ruleFormRef.value.validateField("checkPass", () => null);
        }
      }
    };
    const validatePass2 = (rule: any, value: any, callback: any) => {
      if (value === "") {
        callback(new Error("密码不能为空"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule: any, value: any, callback: any) => {
      if (value === "") {
        callback(new Error("请确认输入密码"));
      } else if (value !== ruleForm.checkPass) {
        ruleForm.chenksPwd = "";
        callback(new Error("跟上次密码不相同，请重新输入"));
      } else {
        callback();
      }
    };
    const ruleForm = reactive({
      email: "",
      checkPass: "",
      chenksPwd: "",
      Code: "", //Code
    });

    const rules = reactive({
      email: [{ validator: validatePass, trigger: "blur" }],
      checkPass: [{ validator: validatePass2, trigger: "blur" }],
      chenksPwd: [{ validator: validatePassword, trigger: "blur" }],
    });

    const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      formEl.validate((valid) => {
        if (valid) {
          console.log("submit!");
        } else {
          console.log("error submit!");
          return false;
        }
      });
    };

    const resetForm = (): void => {
      context.emit("hello");
    };
    return {
      submitForm,
      resetForm,
      rules,
      ruleForm,
      ruleFormRef,
    };
  },
});
</script>

<style lang="less" scoped>
.login {
  // width: 2rem;
  width: 100%;
  /deep/ .el-form {
    text-align: left;
  }
}
</style>